import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
import Home from '@/pages/Home'
import ShopCar from '@/pages/ShopCar'
import Classify from '@/pages/Classify'
import Personal from '@/pages/personal/Personal'
import Search from '@/pages/Search'
import details from '@/pages/Details'
// import Sort from '@/pages/sort/sort'


import Login from '@/pages/Login'
import Registry from '@/pages/Registry'
// <<<<<<< HEAD
// =======
// import PersonSetUp from '@/pages/PersonSetUp'
// import MyOrderForm from '@/pages/MyOrderForm'
// import MyAddres from '@/pages/MyAddres'
import HotHands from '@/components/mall01/HotHands'
import SecondHands from '@/components/mall01/SecondHands'
import CashList from '@/components/mall01/CashList'
import TodayHot from '@/components/mall01/TodayHot'
import MyFish from '@/components/mall01/MyFish'
import RecommendList from '@/components/mall01/RecommendList'
import ContactPeople from '@/components/mall01/ContactPeople'

// >>>>>>> shopMall-1

import PersonSetUp from '@/pages/personal/PersonSetUp'
import MyOrderForm from '@/pages/personal/MyOrderForm'
import MyAddres from '@/pages/personal/MyAddres'
import VueRouter from 'vue-router'
import { Toast } from 'vant';

// import ceshi from '@/components/mall01/ceshi'
Vue.use(Toast);
import { getPersonInformation } from '../api/shop-api/shop-api'

Vue.use(Router)
Vue.use(VueRouter)

// export default new Router({
//   linkActiveClass: 'van-tabbar-item--active',
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/hotHands',
    name: 'HotHands',
    component: HotHands
  },
  {
    path: '/secondHands',
    name: 'SecondHands',
    component: SecondHands
  },
  {
    path: '/cashList',
    name: 'CashList',
    component: CashList
  },
  {
    path: '/todayHot',
    name: 'TodayHot',
    component: TodayHot
  },
  {
    path: '/myFish',
    name: 'MyFish',
    component: MyFish
  },
  {
    path: '/recommendList',
    name: 'RecommendList',
    component: RecommendList
  },
  {
    path: '/contactPeople',
    name: 'ContactPeople',
    component: ContactPeople
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/shopCar',
    name: 'ShopCar',
    component: ShopCar,
    meta: {
      requireLogin: true // 当前路由需要校验，不需要就不用写
    }
  },
  {
    path: '/classify',
    name: 'Classify',
    component: Classify,
  },
  {
    path: '/personal',
    name: 'Personal',
    component: Personal,
    meta: {
      requireLogin: true // 当前路由需要校验，不需要就不用写
    }
  },
  {
    path: '/home/search',
    name: 'Search',
    component: Search
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/registry',
    name: 'Registry',
    component: Registry
  },
  {
    path: '/personSetUp',
    name: 'PersonSetUp',
    component: PersonSetUp
  },
  {
    path: '/myOrderForm',
    name: 'MyOrderForm',
    component: MyOrderForm
  },
  {
    path: '/myAddres',
    name: 'MyAddres',
    component: MyAddres
  },
  {
    path: '/details/:id',
    // name: 'details',
    component: details
  }
]
// export default router
// })
import store from '../store/index'//引入store
const router = new VueRouter({
  linkActiveClass: 'van-tabbar-item--active',
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

let loginPromise = getPersonInformation().then(
  res => {
    console.log(res);
    if (res.code === 0) {
      //登录成功
      store.commit('changeIsLogin', { isLogin: true })
    } else {
      //登录失败
      store.commit('changeIsLogin', { isLogin: false })
      //   routes: [
      //     {
      //       path: '/',
      //       name: 'Home',
      //       component: Home
      //     },
      //     {
      //       path: '/hotHands',
      //       name: 'HotHands',
      //       component: HotHands
      //     },
      //     {
      //       path: '/secondHands',
      //       name: 'SecondHands',
      //       component: SecondHands
      //     },
      //     {
      //       path: '/cashList',
      //       name: 'CashList',
      //       component: CashList
      //     },
      //     {
      //       path: '/todayHot',
      //       name: 'TodayHot',
      //       component: TodayHot
      //     },
      //     {
      //       path: '/myFish',
      //       name: 'MyFish',
      //       component: MyFish
      //     },
      //     {
      //       path: '/recommendList',
      //       name: 'RecommendList',
      //       component: RecommendList
      //     },
      //     {
      //       path: '/contactPeople',
      //       name: 'ContactPeople',
      //       component: ContactPeople
      //     },
      //     {
      //       path: '/home',
      //       name: 'Home',
      //       component: Home
      //     },
      //     {
      //       path: '/shopCar',
      //       name: 'ShopCar',
      //       component: ShopCar
      //     },
      //     {
      //       path: '/classify',
      //       name: 'Classify',
      //       component: Classify
      //     },
      //     {
      //       path: '/personal',
      //       name: 'Personal',
      //       component: Personal
      //     },
      //     {
      //       path: '/home/search',
      //       name: 'Search',
      //       component: Search
      //     },
      //     {
      //       path: '/login',
      //       name: 'Login',
      //       component: Login
      //     },
      //     {
      //       path: '/registry',
      //       name: 'Registry',
      //       component: Registry
      //     },
      //     {
      //       path: '/personSetUp',
      //       name: 'PersonSetUp',
      //       component: PersonSetUp
      //     },
      //     {
      //       path: '/myOrderForm',
      //       name: 'MyOrderForm',
      //       component: MyOrderForm
      //     },
      //     {
      //       path: '/myAddres',
      //       name: 'MyAddres',
      //       component: MyAddres
      // >>>>>>> shopMall-1
    }
  }
).catch()

store.commit('changeLohinPromise', { loginPromise })
router.beforeEach((to, from, next) => {
  // ...
  // console.log('拦截')
  // debugger

  if (to.meta.requireLogin) {
    // Toast('请先登录');
    // console.log(store.commit('changeIsLogin',{isLogin:false})  )
    store.state.loginPromise.then(() => {
      if (store.state.isLogin === false) {
        next('/login')
      } else {
        next()
      }
    })

    // next()

  } else {
    next()

  }

})
export default router

